{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "keras_3.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "private_outputs": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yejianfeng2014/AI/blob/master/keras_3.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "JBP9PXbaIfNh",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "tf.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "tWJko5N8Izz4",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "在Keras中，每个layer instance 都可以被看成是一个函数，其输入是一个tensor，输出也是一个tensor。例如在下面这个实现全连接网络的例子中，你可以看到第一个Dense层的输入是inputs，其输出是x，而且这个x又被当做是第二个Dense层的输入。最初的输入tensor和最后的输出tensor共同定义了模型。而模型的训练方法则跟Sequential model中的情况一致。\n"
      ]
    },
    {
      "metadata": {
        "id": "rvv0M8z-ItuL",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from keras.layers import Input,Dense\n",
        "from keras.models import Model\n",
        "# this return a tensor\n",
        "inputs = Input(shape=(784,))\n",
        "\n",
        "# a layer instance is callable on a tensor, and returns a tensor\n",
        "x = Dense(64, activation = 'relu')(inputs)\n",
        "\n",
        "x= Dense(64,activation = 'relu')(x)\n",
        "\n",
        "predictions = Dense(10,activation = 'softmax')(x)\n",
        "\n",
        "# This creates a model that includes\n",
        "# the Input layer and three Dense layers\n",
        "\n",
        "model =Model(input = inputs,outputs = predictions)\n",
        "\n",
        "model.compile (optimizer = 'rmsprop',loss = 'categorical_crossentropy',metrix = ['accuracy'])\n",
        "\n",
        "# model.fit(data,label)\n",
        "\n",
        "\n",
        "\n",
        "model.summary()\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "yQltIKrJMCDP",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "“多输入-多输出”模型\n",
        "---\n",
        "![avatar][doge] \n",
        "\n",
        "[doge]:\n",
        "\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "c0dpgWwvJPs9",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from keras.layers import concatenate\n",
        "x_in  = Input(shape = (100,) ,name = 'x_in')\n",
        "\n",
        "y_in = Input(shape = (100,) ,name = 'y_in')\n",
        "\n",
        "x = Dense (64,activation = 'relu')(x_in)\n",
        "\n",
        "y = Dense( 64,activation = 'relu')(y_in)\n",
        "\n",
        "z = concatenate ([x,y])\n",
        "\n",
        "x = Dense(1,activation = 'sigmoid' ,name = 'x_out')(z)\n",
        "\n",
        "y = Dense(10,activation = 'softmax', name = 'y_out')(z)\n",
        "\n",
        "\n",
        "model = Model(inputs=[x_in, y_in], outputs=[x, y])\n",
        " \n",
        "model.summary()\n",
        "\n",
        "\n",
        "\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "WiY3flHfPvb5",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from keras.utils import to_categorical\n",
        " \n",
        "import numpy as np\n",
        "data = np.random.random((1000, 100))\n",
        "xs = np.random.randint(2, size=(1000, 1))\n",
        "ys = np.random.randint(10, size=(1000, 1))\n",
        " \n",
        "model.compile(optimizer='rmsprop', loss=['binary_crossentropy', 'categorical_crossentropy'],\n",
        "              loss_weights=[1., 0.2])\n",
        " \n",
        "model.fit([data, data], [xs, to_categorical(ys)],\n",
        "          epochs=10, batch_size=32)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "1bPcM-PP0Z8i",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "你也可以使用字典 (refering to the names of the output tensors)："
      ]
    },
    {
      "metadata": {
        "id": "1x22GVU2z97G",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "model.compile(optimizer='rmsprop',\n",
        "              loss={'x_out': 'binary_crossentropy', 'y_out': 'categorical_crossentropy'},\n",
        "              loss_weights={'x_out': 1., 'y_out': 0.2})\n",
        " \n",
        "# And trained it via:\n",
        "model.fit({'x_in': data, 'y_in': data},\n",
        "          {'x_out': xs, 'y_out': to_categorical(ys)},\n",
        "          epochs=1, batch_size=32)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "xx1Y_E9z1sRO",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "共享层"
      ]
    },
    {
      "metadata": {
        "id": "M0oTLlwx1sQQ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        ""
      ]
    },
    {
      "metadata": {
        "id": "w6uHpS6M0TzR",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "inputs = Input(shape=(64,))\n",
        "\n",
        "# a layer instance is callable on a tensor, and returns a tensor\n",
        "layer_we_share = Dense(64, activation='relu')\n",
        "\n",
        "# Now we apply the layer twice\n",
        "x = layer_we_share(inputs)\n",
        "x = layer_we_share(x)\n",
        "\n",
        "predictions = Dense(10, activation='softmax')(x)\n",
        "\n",
        "model = Model(inputs=inputs, outputs=predictions)\n",
        "model.compile(optimizer='rmsprop',\n",
        "              loss='categorical_crossentropy',\n",
        "              metrics=['accuracy'])\n",
        "\n",
        "model.summary()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "mhaJ5VQ51xL6",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}